package com.printeverywhere.util;

import java.util.Collections;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.ImpComparator.SortAnnouncement;
import com.printeverywhere.ImpComparator.SortItem;
import com.printeverywhere.dto.Announcement;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.MailServer;

public class Sort {

	@SuppressWarnings({ "unchecked", "unchecked" })
	public static List<Items> byItemTitle(List<Items> list){
		SortItem.SortByTitle byTitle = new SortItem().new SortByTitle();
		Collections.sort(list , byTitle);
		if(ActionContext.getContext().getSession().get("rev") != null){
			Collections.sort(list , Collections.reverseOrder(byTitle));
			ActionContext.getContext().getSession().put("rev", null);
		}else{
			ActionContext.getContext().getSession().put("rev", "rev");
		}
		return list;
	}

	@SuppressWarnings({ "unchecked", "unchecked" })
	public static List<Items> byItemTime(List<Items> list){
		SortItem.SortByTime byTime = new SortItem().new SortByTime();
		Collections.sort(list , byTime);
		if(ActionContext.getContext().getSession().get("rev") != null){
			Collections.sort(list , Collections.reverseOrder(byTime));
			ActionContext.getContext().getSession().put("rev", null);
		}else{
			ActionContext.getContext().getSession().put("rev", "rev");
		}
		return list;
	}

	@SuppressWarnings({ "unchecked", "unchecked" })
	public static List<Items> byItemBuyPrice(List<Items> list){
		SortItem.SortByBuyPrice byBuyPrice = new SortItem().new SortByBuyPrice();
		Collections.sort(list , byBuyPrice);
		if(ActionContext.getContext().getSession().get("rev") != null){
			Collections.sort(list , Collections.reverseOrder(byBuyPrice));
			ActionContext.getContext().getSession().put("rev", null);
		}else{
			ActionContext.getContext().getSession().put("rev", "rev");
		}
		return list;
	}

	public static  List<MailServer> byMailDate(List<MailServer> list){
		SortItem.SortMailByReciveDate byMailDate = new SortItem().new SortMailByReciveDate();
		Collections.sort(list , Collections.reverseOrder(byMailDate));
		return list;
	}

	@SuppressWarnings({ "unchecked", "unchecked" })
	public static List<Items> byItemBidSize(List<Items> list){
		SortItem.SortByBidSize byBidSize = new SortItem().new SortByBidSize();
		Collections.sort(list , byBidSize);
		if(ActionContext.getContext().getSession().get("rev") != null){
			Collections.sort(list , Collections.reverseOrder(byBidSize));
			ActionContext.getContext().getSession().put("rev", null);
		}else{
			ActionContext.getContext().getSession().put("rev", "rev");
		}
		return list;
	}

	@SuppressWarnings({ "unchecked", "unchecked" })
	public static List<Items> byItemImage(List<Items> list){
		SortItem.SortByImage byItemImage = new SortItem().new SortByImage();
//		Collections.sort(list , Collections.reverseOrder() byItemImage);

		if(ActionContext.getContext().getSession().get("rev") != null){
			Collections.sort(list , Collections.reverseOrder(byItemImage));
			ActionContext.getContext().getSession().put("rev", null);
		}else{
			ActionContext.getContext().getSession().put("rev", "rev");
		}
		return list;
	}

	@SuppressWarnings({ "unchecked", "unchecked" })
	public static List<Announcement> byAnnouncementDate(List<Announcement> list){
		SortAnnouncement.SortByDate byAnnouncementDate = new SortAnnouncement().new SortByDate();

		if(ActionContext.getContext().getSession().get("rev") != null){
			Collections.sort(list , Collections.reverseOrder(byAnnouncementDate));
			ActionContext.getContext().getSession().put("rev", null);
		}else{
			ActionContext.getContext().getSession().put("rev", "rev");
		}
		return list;
	}
}

